package org.ichow.scenegame.view
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import org.ichow.debug.Log;
	import org.ichow.scenegame.events.SceneEvent;
	import org.ichow.scenegame.model.AssetsProxy;
	import org.ichow.scenegame.notis.HelpNoti;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * @author M.ichow
	 */
	public class HelpMediator extends Mediator
	{
		public static const NAME : String = "HelpMediator";
		public static const HELP_SHOW : String = "helpShow";
		public static const HELP_HIDE : String = "helpHide";
		private var _helpMc:Sprite;
		
		public function HelpMediator( help : HelpNoti = null )
		{
			super( NAME , help );
		}
		
		override public function onRegister() : void
		{
			super.onRegister();
			helpNoti.addEventListener( "close" , onClose );
			
			_helpMc = new Sprite();
			var scene : SceneMediator = facade.retrieveMediator( SceneMediator.NAME ) as SceneMediator;
			var assets : AssetsProxy = facade.retrieveProxy( AssetsProxy.NAME ) as AssetsProxy;
			var w : int = int( scene.getValueForKey( "width" ) );
			var h : int = int( scene.getValueForKey( "height" ) );
			var noti : DisplayObject = assets.getSkin( "GameHelp_mc" );
			helpNoti.bind( noti , _helpMc );
			helpNoti.update( { width: w , height: h , x: 0 , y: 0 } );
		}
		
		private function onClose( e : Event ) : void
		{
			sendNotification( HELP_HIDE );
		}
		
		override public function listNotificationInterests() : Array
		{
			return [ HELP_SHOW , HELP_HIDE , SceneEvent.ADD_TO_SCENE ];
		}
		
		override public function handleNotification( notification : INotification ) : void
		{
			super.handleNotification( notification );
			switch ( notification.getName() )
			{
				case SceneEvent.ADD_TO_SCENE:
					var scene:DisplayObjectContainer = notification.getBody() as DisplayObjectContainer;
					if ( !scene.contains( _helpMc ) )
						scene.addChild( _helpMc );
					Log.trace("++添加帮助层");
					break;
				case HELP_HIDE: 
					helpNoti.cancel();
					Log.trace("帮助: hide");
					break;
				case HELP_SHOW: 
					helpNoti.active();
					Log.trace("帮助: show");
					break;
			}
		}
		
		protected function get helpNoti() : HelpNoti
		{
			return viewComponent as HelpNoti;
		}
	
	}

}